package com.wytec.xywzxw.utils;

import cn.jiguang.sdk.api.PushApi;
import cn.jiguang.sdk.bean.push.PushSendParam;
import cn.jiguang.sdk.bean.push.PushSendResult;
import cn.jiguang.sdk.bean.push.audience.Audience;
import cn.jiguang.sdk.bean.push.message.notification.NotificationMessage;
import cn.jiguang.sdk.bean.push.options.Options;
import cn.jiguang.sdk.constants.ApiConstants;
import cn.jiguang.sdk.enums.platform.Platform;
import com.alibaba.fastjson.JSON;
import com.wytec.xywzxw.bean.PushLog;
import com.wytec.xywzxw.bean.SendPushBean;
import com.wytec.xywzxw.service.PushService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.*;


@Component
public class JPushUtils {

    @Value("${jpush.app_key}")
    private String appKey;
    @Value("${jpush.app_secret}")
    private String appSecret;
    @Value("${jpush.prod}")
    private Boolean prod;


    @Autowired
    PushService pushService;

    /**
     * 推送模型
     */

    public PushSendResult pushWithAlias(SendPushBean sendPushBean) {
        String title = sendPushBean.getTitle();
        String body = sendPushBean.getBody();


        PushApi pushApi = new PushApi.Builder()
                .setAppKey(appKey)
                .setMasterSecret(appSecret)
                .build();

        PushSendParam param = new PushSendParam();
        // 通知内容
        NotificationMessage.Android android = new NotificationMessage.Android();
        android.setAlert(title);
        android.setTitle(body);

        NotificationMessage.IOS iOS = new NotificationMessage.IOS();
        Map<String, String> iOSAlert = new HashMap<>();
        iOSAlert.put("title", title);
        iOSAlert.put("subtitle", body);
        iOS.setAlert(iOSAlert);

        Map<String,Object>extras = new HashMap<>();
        extras.put("extras",sendPushBean.getExtras());

        android.setExtras(extras);
        iOS.setExtras(extras);

        NotificationMessage notificationMessage = new NotificationMessage();
        notificationMessage.setAlert(title);
        notificationMessage.setAndroid(android);
        notificationMessage.setIos(iOS);
        param.setNotification(notificationMessage);

        // 目标人群
        Audience audience = new Audience();

        // 指定目标
        audience.setAliasList(sendPushBean.getAlias());
        param.setAudience(audience);

        // 或者发送所有人
//        param.setAudience(ApiConstants.Audience.ALL);

        // 指定平台
        param.setPlatform(Arrays.asList(Platform.android, Platform.ios));
        // 或者发送所有平台
        // param.setPlatform(ApiConstants.Platform.ALL);

        // Android厂商
        // param.setThirdNotificationMessage();

        // 短信补充
        // param.setSmsMessage();

        // 回调
        // param.setCallback();

        // options
        Options options = new Options();
        options.setApnsProduction(prod);
        param.setOptions(options);

        // 发送
        PushSendResult result = pushApi.send(param);

        // 插入推送日志
        PushLog pushLog = new PushLog();
        pushLog.setBody(body);
        pushLog.setTitle(title);
        pushLog.setAlias(JSON.toJSONString(sendPushBean.getAlias()));
        pushLog.setExtras(JSON.toJSONString(extras));
        pushLog.setSendNo(result.getSendNo());
        pushLog.setMessageId(result.getMessageId());
        pushLog.setEnv(prod ? "prod" : "dev");

        pushService.insertPush(pushLog);

        return result;
    }
}
